package  
{
	import data.*;
	import events.*;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import org.flixel.*;
	
	import ui.GCExperienceBar;
	import ui.textfield.*;

	/**
	 * A container class that will hold information that
	 * the player should always be able to view. This will
	 * include their experience bar, their amount of gold,
	 * and the location of their ship.
	 */
	public class GCInformationBar extends Sprite
	{
		private static const STARTING_X:int = 0;
		private static const STARTING_Y:int = 0;
		
		private var _currentLocation:String;
		private var _quantityGold:int
		private var _currentFameLevel:int;
		private var _currentXP:int;
		private var _xpToNextLevel:int;
		private var _totalWorkers:int;
		
		/*
		 * Displays for information
		 */
		private var _experienceBarArea:GCExperienceBar;
		private var _goldDisplayArea:Sprite;
		private var _goldDisplayText:GCStylizedText;
		private var _locationDisplayArea:Sprite;
		private var _locationDisplayText:GCStylizedText;
		
		private var _xpToNextLevelDisplay:Sprite;
		private var _xpToNextLevelText:GCTextField;
		
		/**
		 * Constructor
		 * @param	x
		 * @param	y
		 */
		public function GCInformationBar(x:int = 0, y:int = 0) 
		{
			this.x = x;
			this.y = y;
			
			_goldDisplayArea = new Sprite();
			_goldDisplayText = new GCStylizedText();
			_locationDisplayArea = new Sprite();
			_locationDisplayText = new GCStylizedText();
			_experienceBarArea = new GCExperienceBar();
			
			_xpToNextLevelDisplay = new Sprite();
			_xpToNextLevelText = new GCTextField();
			
			init();
		}
				
		private function init():void
		{	
			// Gold Display
			_goldDisplayArea.x = _experienceBarArea.x + _experienceBarArea.width;
			_goldDisplayArea.y = STARTING_Y;
			_goldDisplayText.setTextDimension(100, 30);
			_goldDisplayText.setBgStyle(GCStylizedText.STYLE_ONE, 130, 30);
			_goldDisplayArea.addChild(_goldDisplayText);
			
			var goldImage:Bitmap = GCResources.getResource("gold");
			goldImage.height = 30;
			goldImage.width = 30;
			_goldDisplayArea.addChild(goldImage);
			
			// Locaion Display
			_locationDisplayArea.x = _goldDisplayArea.x + _goldDisplayArea.width;
			_locationDisplayArea.y = _goldDisplayArea.y;
			_locationDisplayText.setTextDimension(100, 30);
			_locationDisplayText.setBgStyle(GCStylizedText.STYLE_TWO, 100, 30);
			_locationDisplayArea.addChild(_locationDisplayText);
			
			addChild(_experienceBarArea);
			addChild(_goldDisplayArea);
			addChild(_locationDisplayArea);
			
			
			_xpToNextLevelDisplay.graphics.beginFill(GCConstants.COLOR_GOLD, 0.7);
			_xpToNextLevelDisplay.graphics.drawRoundRect(0, 0, _experienceBarArea.width, 20, 20);
			_xpToNextLevelDisplay.graphics.endFill();
			_xpToNextLevelText.setDimensions(_xpToNextLevelDisplay.width, _xpToNextLevelDisplay.height);
			
			_xpToNextLevelDisplay.addChild(_xpToNextLevelText);
			_experienceBarArea.addEventListener(MouseEvent.MOUSE_OVER, onShowXP);
			_experienceBarArea.addEventListener(MouseEvent.MOUSE_OUT, onHideXP);
		}
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------		
		
		/**
		 * Function to initialize all the data in the information bar.
		 * 
		 * @param	gold
		 * @param	location
		 */
		public function initializeData(gold:int, location:String, level:uint, levelXP:uint):void
		{
			updateGold(gold);
			updateLocation(location);
			updateExperience(level, levelXP);
		}
		
		public function updateGold(newQuantity:int):void
		{
			_goldDisplayText.text = String(newQuantity);
		}
		
		public function updateLocation(newLocation:String):void
		{
			_locationDisplayText.text = newLocation;
		}
		
		public function updateExperience(lvl:uint, xp:uint):void
		{
			_experienceBarArea.setXp(lvl, xp);
		}		
		
		//----------------------------------------------------------------------
		//	EVENT HANDLING FUNCTIONS
		//----------------------------------------------------------------------
		private function onShowXP(event:MouseEvent):void
		{
			if (_xpToNextLevelDisplay.parent == null)
			{
				_xpToNextLevelDisplay.y = -1 * _xpToNextLevelDisplay.height + 10;
				_experienceBarArea.addChild(_xpToNextLevelDisplay);
			}
			_xpToNextLevelDisplay.visible = true;
			var playerLevel:uint = GCPlayState.playerData.playerLevel;
			var xpToNext:uint = GCExperienceData._experienceSequence[playerLevel] - GCPlayState.playerData.lvlXp + 1;
			_xpToNextLevelText.text = String(xpToNext) + " more XP to level " + String(playerLevel + 1);
		}
		
		private function onHideXP(event:MouseEvent):void
		{
			_xpToNextLevelDisplay.visible = false;
		}
	}
}